package com.newly.center.business.api;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.newly.api.business.feign.SiteClient;
import com.newly.center.business.service.SiteService;
import com.newly.common.base.annotation.NewlyLog;
import com.newly.common.base.constants.AuthConstant;
import com.newly.common.base.constants.CodeConstant;
import com.newly.common.base.constants.MenuConstant;
import com.newly.common.base.constants.ServerUriConstant;
import com.newly.common.base.entity.R;
import com.newly.common.base.entity.auth.ICurrentTenant;
import com.newly.common.base.entity.business.Dto.SiteDto;
import com.newly.common.base.entity.business.Po.Site;
import com.newly.common.base.entity.business.Vo.ExcelSite;
import com.newly.common.base.exception.NewlyException;
import com.newly.common.core.utils.ExcelUtil;
import com.newly.common.core.utils.RequestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @anthor sbq
 * @date 2021/10/26 17:37
 */
@RestController
@RequestMapping(ServerUriConstant.NEWLY_BUSINESS_SITE)
public class SiteController implements SiteClient {

    @Autowired
    private RequestUtils requestUtils;

    @Autowired
    SiteService siteService;

    @Override
    public void export(String name,String id, Integer type, HttpServletResponse response) {
        ICurrentTenant currentTenant = requestUtils.getCurrentTenant();
        SiteDto siteDto = new SiteDto();
        siteDto.setName(name);
        siteDto.setId(id);
        siteDto.setType(type);
        siteDto.setPageSize(1);
        int totalCount = siteService.getPage(currentTenant, siteDto).getTotalCount();
        if (totalCount < 1) {
            throw new NewlyException(CodeConstant.FAILED, "无站点信息数据");
        }
        siteDto.setPageSize(totalCount);
        List<Site> list = (List<Site>) siteService.getPage(currentTenant, siteDto).getList();

        List<ExcelSite> collect = list.stream().map(s -> {
            ExcelSite excelSite = BeanUtil.copyProperties(s, ExcelSite.class, "type");
            excelSite.setType("货场");
            excelSite.setCheckDirection("出场");
            return excelSite;
        }).collect(Collectors.toList());

        ExcelUtil.export(response, "站点信息表", null, collect, ExcelSite.class);
    }

    @NewlyLog(ability = MenuConstant.Ability.QUE, title = "站点", isOpen = false)
    @Override
    public R getPage(SiteDto siteDto) {
        ICurrentTenant currentTenant = requestUtils.getCurrentTenant();
        return R.ok(siteService.getPage(currentTenant, siteDto));
    }

    @Override
    public R getDetailsById(Long id, Integer offset) {

        return R.ok(siteService.getDetailsById(id, offset));
    }

    @NewlyLog(ability = MenuConstant.Ability.UPD, title = "站点", isOpen = true)
    @Override
    public R edit(Site site) {

        if (site.getId() == null)
            return R.fail("站点id不能为空");
        this.siteService.edit(site);
        return R.ok();
    }

    @NewlyLog(ability = MenuConstant.Ability.ADD, title = "站点", isOpen = true)
    @Override
    public R save(Site site) {
        this.siteService.insert(site);
        return R.ok();
    }

    @NewlyLog(ability = MenuConstant.Ability.DEL, title = "站点", isOpen = true)
    @Override
    public R delete(Long[] ids) {
        if (ids.length == 0)
            return R.fail("id不能为空");
        return this.siteService.delete(ids);
    }

    @Override
    public R getSiteNames() {
        List<String> siteNames = this.siteService.getSiteNames();
        return R.ok(siteNames);
    }

    @Override
    public R getSiteNameAndId() {
        return R.ok(this.siteService.getSiteNameAndId());
    }

    @Override
    public R repeat(String siteName) {
        if (StringUtils.isBlank(siteName))
            return R.fail("参数站点名称不能为空！");
        return this.siteService.repeat(siteName) ? R.fail(1, "站点名称已存在") : R.ok();
    }

    @NewlyLog(ability = MenuConstant.Ability.QUE, title = "站点", isOpen = false)
    @Override
    public R queryByIdsInAuth(List<String> siteIds) {
        ICurrentTenant currentTenant = requestUtils.getCurrentTenant();
        currentTenant.assertIncludeEntities(AuthConstant.AuthEntityType.SITE, siteIds);

        List<Site> sites = null;
        try {
            sites = siteService.getByIds(siteIds);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
        return R.ok(sites);
    }
}
